import path from "node:path";

const contentTypeMiddleware = (req, res, next) => {
  let contentType = "text/html";
  // 提取url的后缀
  const extname = path.extname(req.url);
  switch (extname) {
    case ".js":
      contentType = "text/javascript";
      break;
    case ".css":
      contentType = "text/css";
      break;
    case ".json":
      contentType = "application/json";
      break;
    case ".png":
      contentType = "image/png";
      break;
    case ".jpg":
      contentType = "image/jpeg";
      break;
  }

  res.setHeader("Content-Type", contentType);
  next();
};

export default contentTypeMiddleware;
